package com.wswy.wzcx.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.common.util.UriUtil;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.wswy.wzcx.R;
import com.wswy.wzcx.ui.hold.ViewHolder;
import com.wswy.wzcx.ui.other.AndroidUtilities;
import com.wswy.wzcx.ui.other.LayoutHelper;
import com.wswy.wzcx.ui.other.ViewHelp;
import com.wswy.wzcx.ui.data.UserInfoMode;

import java.util.List;

/**
 * Created by wangduheng26 on 2018/1/24.
 * wangduheng26@gmail.com
 */

public class UserInfoAdapter extends BaseAdapter<UserInfoMode, UserInfoAdapter.UserInfoVH> {
    public static final int TYPE_TITLE = 1;
    public static final int TYPE_CONTENT = 2;

    public UserInfoAdapter(List<UserInfoMode> list) {
        super(list);
    }

    @Override
    public UserInfoVH onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        final Context context = parent.getContext();
        int paddingLeft = AndroidUtilities.dp(12);
        if (viewType == TYPE_TITLE) {
            TextView textView = new TextView(context);
            textView.setPadding(paddingLeft, 0, paddingLeft, 0);
            textView.setGravity(Gravity.CENTER_VERTICAL);
            textView.setTextColor(ContextCompat.getColor(context, R.color.gray40));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            textView.setMinHeight(AndroidUtilities.dp(36));
            return new TitleVH(textView);
        } else if (viewType == TYPE_CONTENT) {
            RelativeLayout layout = new RelativeLayout(context);
            layout.setBackgroundColor(Color.WHITE);
            layout.setPadding(paddingLeft, 0, paddingLeft, 0);
            layout.setMinimumHeight(AndroidUtilities.dp(54));
            TextView name = new TextView(context);
            name.setGravity(Gravity.CENTER_VERTICAL);
            name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            name.setTextColor(ContextCompat.getColor(context, R.color.gray20));
            layout.addView(name, LayoutHelper.createRelative(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT));
            ImageView imageView = new ImageView(context);
            imageView.setId(ViewHelp.generateViewId());
            imageView.setImageResource(R.drawable.arrow_right_grey);
            layout.addView(imageView, LayoutHelper.createRelative(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT));
            TextView content = new TextView(context);
            content.setGravity(Gravity.CENTER_VERTICAL);
            content.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            content.setTextColor(ContextCompat.getColor(context, R.color.gray60));
            RelativeLayout.LayoutParams params = LayoutHelper.createRelative(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT);
            LayoutHelper.setMargin(params, 0, 0, 22, 0);
            layout.addView(content, params);

            SimpleDraweeView header = new SimpleDraweeView(context);
            GenericDraweeHierarchy hierarchy =
                    GenericDraweeHierarchyBuilder.newInstance(context.getResources())
                            .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                            .setFadeDuration(300)
                            .setRoundingParams(new RoundingParams().setRoundAsCircle(true).setOverlayColor(Color.WHITE))
                            .setFailureImage(R.drawable.avatar_default_grey)
                            .setFailureImageScaleType(ScalingUtils.ScaleType.CENTER)
                            .setPlaceholderImage(R.drawable.avatar_default_grey)
                            .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER)
                            .build();
            header.setHierarchy(hierarchy);
            header.setVisibility(View.GONE);
            RelativeLayout.LayoutParams relative = LayoutHelper.createRelative(40, 40);
            relative.addRule(RelativeLayout.CENTER_VERTICAL);
            relative.setMargins(10,0,10,0);
            relative.addRule(RelativeLayout.LEFT_OF,imageView.getId());
            layout.addView(header, relative);
            RecyclerView.LayoutParams params1 = new RecyclerView.LayoutParams(LayoutHelper.MATCH_PARENT, AndroidUtilities.dp(54));
            layout.setLayoutParams(params1);
            return new ContentVH(layout, name, imageView, content, header);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        final UserInfoMode item = getItem(position);
        if (item != null) {
            return item.isTitle ? TYPE_TITLE : TYPE_CONTENT;
        }
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(UserInfoVH holder, UserInfoMode item, int position) {
        holder.bind(item);
    }

    public static abstract class UserInfoVH extends ViewHolder {

        public UserInfoVH(View itemView) {
            super(itemView);
        }

        public abstract void bind(UserInfoMode item);
    }

    public static class TitleVH extends UserInfoVH {
        public TextView textView;

        public TitleVH(TextView itemView) {
            super(itemView);
            textView = itemView;
        }

        @Override
        public void bind(UserInfoMode item) {
            textView.setText(item.name);
        }
    }

    public static class ContentVH extends UserInfoVH {
        public TextView name, content;
        public ImageView imageView, header;

        public ContentVH(View itemView, TextView name, ImageView imageView, TextView content, ImageView header) {
            super(itemView);
            this.name = name;
            this.imageView = imageView;
            this.content = content;
            this.header = header;
        }

        @Override
        public void bind(UserInfoMode item) {
            name.setText(item.name);
            if (TextUtils.equals(item.name, "头像")) {
                header.setImageURI(UriUtil.parseUriOrNull(item.content));
                header.setVisibility(View.VISIBLE);
                content.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals(item.name, "手机") ) {
                content.setText(item.content);
                header.setVisibility(View.GONE);
                content.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.INVISIBLE);

            } else if (TextUtils.equals(item.name,"微信")){
                content.setText(item.content);
                header.setVisibility(View.GONE);
                content.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);
            }else {
                content.setText(item.content);
                header.setVisibility(View.GONE);
                content.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);

            }

        }
    }

}
